package org.jeecg.modules.demo.periodicwork.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.Constants;
import org.jeecg.modules.demo.periodicwork.entity.CoopDeptSuper;
import org.jeecg.modules.demo.periodicwork.entity.TobedonePeriodicWork;
import org.jeecg.modules.demo.periodicwork.service.ITobedonePeriodicWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name="配合部门接收周期性工作")
@RestController
@RequestMapping("/tobedonePeriodicWork/tobedonePeriodicWork")
@Slf4j
public class TobedonePeriodicWorkController {
    @Autowired
    private ITobedonePeriodicWorkService tobedonePeriodicWorkService;
    /**
     * 分页列表查询
     *
     * @return
     */
    //@AutoLog(value = "待办-分页列表查询")
    @Operation(summary="待办-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TobedonePeriodicWork>> queryPageList(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req) {
        /*
         * 添加判断，待办功能只有在周期性工作列表中存在该部门为配合部门才显示
         * */
        PageHelper.startPage(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<TobedonePeriodicWork> TobedonePeriodicWorkList = tobedonePeriodicWorkService.SelectTobedonePeriodicWorkListByOrgId(sysUser.getOrgId(),Constants.TODO);
        // 创建PageInfo对象
        PageInfo<TobedonePeriodicWork> pageInfo = new PageInfo<>(TobedonePeriodicWorkList);
        // 转换为IPage对象
        IPage<TobedonePeriodicWork> page = new Page<>(pageNo, pageSize, pageInfo.getTotal());
        page.setRecords(pageInfo.getList());
        return Result.OK(page);
    }



    /*
接收任务
* */
    @RequiresPermissions("tobedonePeriodicWork:yx_tobedonePeriodicWork:receiveTask")
    @Transactional//事务处理注解
    @RequestMapping(value = "/receiveTask", method = RequestMethod.POST)
    public Result<String> receiveTask() {
        //点击接收，弹出配合主管的框，填写完成之后点击确定
        //修改该记录为已接收
        //应该在子表中添加一个配合主管部门为配合部门，状态为待办的记录
        //让配合主管去接收任务



        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //根据当前的部门id去找sys_depart中的org_code，去连接sys_user中的org_code
        //根据org_code去查询sys_user中的id和username。
        //将查询到的id和username显示到前端，让用户选择配合主管。
        List<CoopDeptSuper> CoopDeptSuperList =tobedonePeriodicWorkService.SelectCoopDeptSuper(sysUser.getOrgId());
        System.out.println(CoopDeptSuperList);
        //弹出一个框，将列表显示出来，用户选择，并在主表中将sys_user中的id存进到配合主管中去，并且将配合主管状态改为待办。
//        for (TobedonePeriodicWork t : TobedonePeriodicWorkList) {
//            //修改本记录为已接收
//            if (t.getCoop_depart().equals(sysUser.getOrgId())) {
//                // 根据kpi_id查询yx_tobedone表中的记录
//                QueryWrapper<TobedonePeriodicWork> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("kpi_id", t.getKpiId());
//                queryWrapper.eq("coop_depart", sysUser.getOrgId());
//                TobedonePeriodicWork toUpdate = tobedonePeriodicWorkService.getOne(queryWrapper);
//                if (toUpdate != null) {
//                    toUpdate.setCoop_depart_taskstatus(Constants.RECEIVE);
//                    tobedonePeriodicWorkService.updateById(toUpdate);
//                }
//            }
//        }
        return Result.OK(Constants.RECEIVE_SUCCESS);
    }
}
